package com.qiwenshare.file.util;

import cn.hutool.core.util.IdUtil;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.common.util.security.SessionUtil;
import com.qiwenshare.file.domain.UserFile;
import com.qiwenshare.file.io.QiwenFile;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class QiwenFileUtil {


    public static UserFile getQiwenDir(String userId, String filePath, String fileName) {
        UserFile userFile = new UserFile();
        userFile.setUserFileId(IdUtil.getSnowflakeNextIdStr());
        userFile.setUserId(userId);
        userFile.setFileId(null);
        userFile.setFileName(fileName);
        userFile.setFilePath(QiwenFile.formatPath(filePath));
        userFile.setExtendName(null);
        userFile.setIsDir(1);
        userFile.setUploadTime(DateUtil.getCurrentTime());
        userFile.setCreateUserId(SessionUtil.getUserId());
        userFile.setCreateTime(DateUtil.getCurrentTime());
        userFile.setDeleteFlag(0);
        userFile.setDeleteBatchNum(null);
        return userFile;
    }

    public static UserFile getQiwenFile(String userId, String fileId, String filePath, String fileName, String extendName) {
        UserFile userFile = new UserFile();
        userFile.setUserFileId(IdUtil.getSnowflakeNextIdStr());
        userFile.setUserId(userId);
        userFile.setFileId(fileId);
        userFile.setFileName(fileName);
        userFile.setFilePath(QiwenFile.formatPath(filePath));
        userFile.setExtendName(extendName);
        userFile.setIsDir(0);
        userFile.setUploadTime(DateUtil.getCurrentTime());
        userFile.setCreateTime(DateUtil.getCurrentTime());
        userFile.setCreateUserId(SessionUtil.getUserId());
        userFile.setDeleteFlag(0);
        userFile.setDeleteBatchNum(null);
        return userFile;
    }

    public static UserFile searchQiwenFileParam(UserFile userFile) {
        UserFile param = new UserFile();
        param.setFilePath(QiwenFile.formatPath(userFile.getFilePath()));
        param.setFileName(userFile.getFileName());
        param.setExtendName(userFile.getExtendName());
        param.setDeleteFlag(0);
        param.setUserId(userFile.getUserId());
        param.setIsDir(0);
        return param;
    }

    public static String formatLikePath(String filePath) {
        String newFilePath = filePath.replace("'", "\\'");
        newFilePath = newFilePath.replace("%", "\\%");
        newFilePath = newFilePath.replace("_", "\\_");
        return newFilePath;
    }

    public static String repSpaceToComma(String name){
        if(StringUtils.isBlank(name))
            return null;
        String temp = name.replaceAll("[\\u3001\\uff1b\\uff0c\\uff1a\\u201c\\uff01\\;\\+\\?\\#\\&\\$\\*\\%\\:\\\"]",",");
        String[] b = temp.split("\\s+");
        String c = StringUtils.join(b,",");
        String[] d = c.split(",");
        List e = Arrays.stream(d).filter(s -> !s.trim().equals("")).collect(Collectors.toList());
        return StringUtils.join(e,",");
    }

    public static List<String> parseName(String name){
        List<String> list = new ArrayList<>();
        if (StringUtils.isBlank(name))
            return list;
        name = name.trim();
        String name2 = RegExUtils.replaceFirst(name, "[\\uff0c\\,]*", "");
        String[] names = name2.split("[\\uff0c\\,]");
        for(int i =0;i<names.length;i++){
            String[] tempNames = (i>0?"&"+names[i]:names[i]).split("\\s+");
            boolean flag = false;
            for(int j =0;j<tempNames.length;j++){
                if(!tempNames[j].startsWith("&") && j!=0 && !flag)
                    list.add(("|"+tempNames[j]));
                else if (tempNames[j].equals("&")) {
                    flag = true;
                    continue;
                }else if(flag) {
                    list.add(("&"+tempNames[j]));
                    flag = false;
                }else
                    list.add(tempNames[j]);
            }
        }
        return list;
    }
}
